package abs.sensor;

import java.text.DecimalFormat;

import android.app.Activity;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.widget.TextView;

public class OrActivity extends Activity implements SensorEventListener {
	TextView accl_x, accl_y, accl_z;
	TextView ornt_x, ornt_y, ornt_z;
	TextView magf_x, magf_y, magf_z;
	TextView pres, mass;
	TextView lght, temp, prox;
	
	SensorManager sensmang;
	
	Sensor accelerometer,
		   orientation,
		   magnetic_field,
		   light,
		   temperature,
		   proximity,
		   pressure;
	
	
	DecimalFormat round = new DecimalFormat("0.000000");
	
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.or);
        
		 //Acquire SensorManager
	    sensmang = (SensorManager) getSystemService(SENSOR_SERVICE);
	    
	    //Accelerometer Sensor
        accelerometer = sensmang.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
        accl_x = (TextView) findViewById(R.id.accelerometer_x);
        accl_y = (TextView) findViewById(R.id.accelerometer_y);
        accl_z = (TextView) findViewById(R.id.accelerometer_z);
        
        //Orientation Sensor
        orientation = sensmang.getDefaultSensor(Sensor.TYPE_ORIENTATION);
        ornt_x = (TextView) findViewById(R.id.orientation_x);
        ornt_y = (TextView) findViewById(R.id.orientation_y);
        ornt_z = (TextView) findViewById(R.id.orientation_z);
	    
	    /*Pressure Sensor
	    pressure = sensmang.getDefaultSensor(Sensor.TYPE_PRESSURE);
	    pres = (TextView) findViewById(R.id.pressure);
	    mass = (TextView) findViewById(R.id.mass);
	    */
	}
	
	public void onAccuracyChanged(Sensor sensor, int accuracy) {
		// TODO Auto-generated method stub
		
	}
	
	public void onSensorChanged(SensorEvent event) {
		int sensor_type = event.sensor.getType();
		synchronized (this) {
			if (sensor_type == Sensor.TYPE_ACCELEROMETER) {
				accl_x.setText(round.format(event.values[0]) + " m/s^2");
				accl_y.setText(round.format(event.values[1]) + " m/s^2");
				accl_z.setText(round.format(event.values[2]) + " m/s^2");
			}
			if (sensor_type == Sensor.TYPE_ORIENTATION) {
				ornt_x.setText(round.format(event.values[0]));
				ornt_y.setText(round.format(event.values[1]));
				ornt_z.setText(round.format(event.values[2]));
			}
			if (sensor_type == Sensor.TYPE_MAGNETIC_FIELD) {
				magf_x.setText(round.format(event.values[0]) + " uT");
				magf_y.setText(round.format(event.values[1]) + " uT");
				magf_z.setText(round.format(event.values[2]) + " uT");
			}
			if (sensor_type == Sensor.TYPE_LIGHT) {
				lght.setText(new DecimalFormat("0.00").format(event.values[0]) + " lux");
			}
			if (sensor_type == Sensor.TYPE_TEMPERATURE) {
				temp.setText(Float.toString(event.values[0]) + " C // " + new DecimalFormat("0.0").format(event.values[0]*1.8 + 32) + " F");
			}
			if (sensor_type == Sensor.TYPE_PROXIMITY) {
				if (event.values[0] <= 3) {
					prox.setText("NEAR");
				} else {
					prox.setText("FAR");
				}
			}
			if (sensor_type == Sensor.TYPE_PRESSURE) {
				prox.setText(round.format(event.values[0]) + "");
			}
	    }
		
	}
	
	@Override
	protected void onResume() {
	    super.onResume();
	    sensmang.registerListener(this, 
	    		                  accelerometer, 
	    		                  SensorManager.SENSOR_DELAY_GAME);
	    sensmang.registerListener(this, 
				                  orientation, 
				                  SensorManager.SENSOR_DELAY_GAME);
	}
	
	@Override
	protected void onStop() {
	    sensmang.unregisterListener(this);
	    super.onStop();
	}    
}